package com.jwgf.canstate;

import java.util.Arrays;

import com.jwgf.smarowindingmachine_unity.ui.SmaroWindingMachineActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class CanHandShakeTest {

	public CanHandShakeTest() {
		EventBus.getDefault().register(this);
	}

	@Subscribe
	public void OnCanRecvEvent(byte[] data) {
		byte[] getBroadcast = { 0x00, 0x55, (byte) 0xAA, 0x01, 0, 0, 0, 0, 0, 0 };
		byte[] getACK = { 0x00, 0x01, (byte) 0xE0, 0x02, (byte) 0xAA, 0x01, 0, 0, 0, 0 };
		byte[] releaseBroadcast = { 0x00, 0x55, 0x55, 0x01, 0, 0, 0, 0, 0, 0 };
		byte[] releaseACK = { 0x00, 0x01, (byte) 0xE0, 0x02, 0x55, 0x01, 0, 0, 0, 0 };
		if (Arrays.equals(data, getBroadcast)) {
			SmaroWindingMachineActivity.canHandStack.process(getACK);
		} else if (Arrays.equals(data, releaseBroadcast)) {
			SmaroWindingMachineActivity.canHandStack.process(releaseACK);
		} else {
			byte[] ACK = { 0x00, 0x01, (byte) 0xE0, data[1], data[2], 0x01, 0, 0, 0, 0 };
			if (ACK[2] == (byte)0xE0){
				SmaroWindingMachineActivity.canHandStack.process(ACK);
			} else {
				byte[] QueryACK = {0x00, 0x01,data[4],data[1], (byte) 0xaa,(byte) 0xbb,(byte) 0xcc,(byte) 0xdd,(byte) 0xee,(byte) 0xff};
				SmaroWindingMachineActivity.canHandStack.process(QueryACK);
			}
		}
	}
}
